import { currentMonthTotal, pointsOrderslist } from "../../server/api";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    show: false,
    type:wx.getStorageSync('count'),
    minDate: new Date(1940, 0, 1).getTime(),
    currentDate: new Date().getTime(),
    sjheight:wx.getMenuButtonBoundingClientRect().top ,
    jnheight: wx.getMenuButtonBoundingClientRect().height ,
    year: "",
    month: "",
    jifen: "",
    pageNo: "1",
    pageSize: "10",
    type:'',
    formatter(type, value) {
      if (type === "year") {
        return `${value}`;
      }
      if (type === "month") {
        return `${value}月`;
      }
      return value;
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({type:options.type})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      list: [],
      pageNo: 1,
      pageSize: 10,
    });
    this.getTabBar().toggleMenu(2);
    this.getTabBar().init();
    const now = new Date();
    this.setData({
      year: now.getFullYear(),
      month: now.getMonth() + 1,
      list: [],
    });
    this.getdata();
    this.getlist();
  },
  //获取当月积分
  async getdata() {
    const params = {
      time:
      this.data.year +
      "-" +
      (this.data.month < 10 ? "0" + this.data.month : this.data.month),
    };
    const data = await currentMonthTotal(params);
    if (data.code == 200) {
      this.setData({ jifen: data.result });
    }
  },
  //获取积分列表
  async getlist(value) {

    const params = {
      pageNo: value == undefined ? this.data.pageNo : value.pageNo,
      pageSize: value == undefined ? this.data.pageSize : value.pageSize,
      time:
        this.data.year +
        "-" +
        (this.data.month < 10 ? "0" + this.data.month : this.data.month),
    };
    const res = await pointsOrderslist(params);
    if (res.code == 200) {
      this.setData({ list: [...this.data.list, ...res.result.records] });
    }
  },
  getMore() {
    this.data.pageNo++;
    let params = {
      pageNo: this.data.pageNo,
      pageSize: this.data.pageSize,
    };
    this.getlist(params);
  },
  //日期选择事件
  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
  },
  //取消日期
  onCancel() {
    this.setData({ show: false });
  },
  //日期确认事件
  onConfirm(val) {
    this.setData({
      list: [],
      pageNo: 1,
      pageSize: 10,
    });
    this.setData({ show: false });
    this.setData({
      year: this.timeFormat(val.detail),
      month: this.monthFormat(val.detail),
    });
    this.getlist();
    this.getdata();
  },
  //转年
  timeFormat(time) {
    // 时间格式化 2019-09-08
    var time = new Date(time);
    let year = time.getFullYear();
    return year;
  },
  //转月
  monthFormat(time) {
    var time = new Date(time);
    let month = time.getMonth() + 1;
    return month;
  },
  taptime() {
    this.setData({ show: true });
  },
  black(){
    if(this.data.type == 6)
    {
      wx.switchTab({
        url: '/pages/merchant/merchant',
      })
    }else
    {
      wx.switchTab({
        url: '/pages/merchanthome/merchanthome',
      })
    }
   
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    wx.removeStorageSync('count')
    this.setData({type:''})
  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    this.setData({type:''})
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
